﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlBaoCaoTonKho : System.Web.UI.UserControl
{
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    BCDataCenter.DataAccessLayer.DAL dt;
    SqlConnection con;
    SqlDataAdapter da;
    DataTable dtb;
    MethodClassCommon common;
    SqlCommand command;
    static bool flag = false;
    static string ngaychot=null,error ="";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
                common = new MethodClassCommon();
                string ClosedDate = common.GetDataDepend("get_CloseDate", "Sitecode", Session["Sitecode"].ToString());
                DateTime dateTest;
                System.Globalization.DateTimeFormatInfo sFormat = new System.Globalization.DateTimeFormatInfo();
                sFormat.ShortDatePattern = "dd/MM/yyyy";
                dateTest = Convert.ToDateTime(ClosedDate, sFormat);
                TextBox1.Text = dateTest.Date.AddDays(1).ToString("dd/MM/yyyy");
                ngaychot = TextBox1.Text;
            }
            else
            {
                if (MultiView1.ActiveViewIndex == 0)
                {
                    flag = false;
                }
                else
                {
                    if (flag == true)
                    {
                        bindReport();
                    }
                }
            }
        }
        catch
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
   
    void bindReport()
    {
        dt  = new BCDataCenter.DataAccessLayer.DAL();
        ReportBaoCaoTonKho rp = new ReportBaoCaoTonKho();
        con = new SqlConnection(str);
        con.Open();
        command = new SqlCommand("GET_SiteName_Address", con);
        command.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr;
        command.Parameters.Add(new SqlParameter("@sitecode",Session["Sitecode"].ToString()));
        dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        rp.Parameters["ttpp"].Value = dr[0].ToString();
        rp.Parameters["diachi"].Value = dr[1].ToString();
        rp.Parameters["ngay"].Value = TextBox1.Text;
        dr.Close();
        DataSet ds = new DataSet();
        dtb=new DataTable();
        common = new MethodClassCommon();
        rp.Parameters["kho"].Value = common.GetDataDepend("get_location", "sitecode", Session["Sitecode"].ToString());
        string table = "DIN" + TextBox1.Text.Substring(3, 2) + TextBox1.Text.Substring(6);
        string date = TextBox1.Text.ToString();
        string location = dt.MyExecuteReaders(ref error, "SELECT     location FROM INLOCD WHERE     (SiteCode = '" + Session["Sitecode"].ToString() + "')", CommandType.Text);
        if (TextBox1.Text != ngaychot)
        {
            //string table = "DIN" + TextBox1.Text.Substring(3, 2) + TextBox1.Text.Substring(6);
            //string date = TextBox1.Text.ToString();
            //string location = dt.MyExecuteReaders(ref error, "SELECT     location FROM INLOCD WHERE     (SiteCode = '" + Session["Sitecode"].ToString() + "')", CommandType.Text);
            if (CheckBox1.Checked)
            {

                ds = dt.MyExecuteQuery(ref error, "GET_QTY_DINyymm_0", CommandType.StoredProcedure, new SqlParameter("@TableName", table), new SqlParameter("@date", date), new SqlParameter("@location", location));
            }
            else
            {
                ds = dt.MyExecuteQuery(ref error, "GET_QTY_DINyymm", CommandType.StoredProcedure, new SqlParameter("@TableName", table), new SqlParameter("@date", date), new SqlParameter("@location", location));
            }
        }
        //    {
        //        da = new SqlDataAdapter("GET_QTY_DINyymm_0", con);

        //    }
        //    else
        //    {
        //        da = new SqlDataAdapter("GET_QTY_DINyymm", con);
        //    }
        //    da.SelectCommand.Parameters.Add(new SqlParameter("@TableName", table));
        //    da.SelectCommand.Parameters.Add(new SqlParameter("@date", data));
        //}
        else
        {
            if (CheckBox1.Checked)
            {

                ds = dt.MyExecuteQuery(ref error, "P_Get_Qty", CommandType.StoredProcedure, new SqlParameter("@location", location)); 
            }
            else
            {
                ds = dt.MyExecuteQuery(ref error, "P_Get_Qty_qtyonhand", CommandType.StoredProcedure, new SqlParameter("@location", location)); 
                //da = new SqlDataAdapter("P_Get_Qty_qtyonhand", con);
            }
        }
        //da.SelectCommand.CommandType = CommandType.StoredProcedure;
        //da.SelectCommand.Parameters.Add(new SqlParameter("@location",common.GetDataDepend("get_location", "sitecode", Session["Sitecode"].ToString())));
        try
        {
            //da.Fill(dtb);
            rp.DataSource = ds.Tables[0];
            this.ReportViewer1.Report = rp;
            this.ReportViewer1.DataBind();
            flag = true;
            MultiView1.ActiveViewIndex = 1;
        }
        catch
        {
            uscMsgBox1.AddMessage("Không có table tương ứng",uscMsgBox.enmMessageType.Error);
        }
       

    }
   
    protected void Button1_Click1(object sender, EventArgs e)
    {
        try
        {
            bindReport();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
           
    }
    
}
